/**
 * 
 */
package com.excalibur.core.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.excalibur.core.util.StringHelper;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年3月10日
 * 
 * @time 下午7:02:34
 * 
 * @desc
 * 
 */
public class DateUtils {

	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 
	 */
	public DateUtils() {

	}

	/**
	 * 与当前系统时间相比，是否为一个过去的时间
	 * 
	 * @param date
	 */
	public static boolean isPassedDate(Date date) {
		if (date == null) {
			return false;
		}
		return (new Date()).after(date);
	}

	/**
	 * @param dateTimeStr
	 * @param format
	 * @return
	 */
	public static Date parseFromString(String dateTimeStr, String format) {
		if (StringUtils.isBlank(dateTimeStr)) {
			return null;
		}
		if (StringHelper.isBlank(format)) {
			format = DEFAULT_DATETIME_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dateTimeStr
	 * @return
	 */
	public static Date parseFromString(String dateTimeStr) {
		return parseFromString(dateTimeStr, DEFAULT_DATETIME_FORMAT);
	}

	/**
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatAsString(Date date, String format) {
		if (date == null) {
			return null;
		}
		if (StringHelper.isBlank(format)) {
			format = DEFAULT_DATETIME_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * @param date
	 * @return
	 */
	public static String formatAsString(Date date) {
		return formatAsString(date, DEFAULT_DATETIME_FORMAT);
	}

	/**
	 * 检查起始日期与截止日期之差是否在一定范围内.
	 * 
	 * @param start
	 * @param end
	 * @param field
	 *            i.e. Calendar.MONTH
	 * @param amount
	 * @return
	 */
	public static boolean isInBoundary(Date start, Date end, int field, int amount) {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();

		startCalendar.setTime(start);
		endCalendar.setTime(end);
		startCalendar.add(field, amount);

		return !startCalendar.before(endCalendar);
	}

	/**
	 * 根据特定的单位，计算两个日期之间的时间差
	 * 
	 * @param start
	 * @param end
	 * @param field
	 *            日期单位，如Field.SECOND表示计算两个日期之间相差的秒数，四舍五入.
	 * @return
	 */
	public static long calcDuration(Date start, Date end, Field field) {
		switch (field) {
		case MILLISECOND: {
			long durMilliSec = end.getTime() - start.getTime();
			return durMilliSec;
		}
		case SECOND: {
			long durMilliSec = end.getTime() - start.getTime();
			return Math.round((double) durMilliSec / 1000);
		}
		case MINUTE: {
			long durMilliSec = end.getTime() - start.getTime();
			return Math.round((double) durMilliSec / 1000 / 60);
		}
		case HOUR: {
			long durMilliSec = end.getTime() - start.getTime();
			return Math.round((double) durMilliSec / 1000 / 60 / 60);
		}
		case DAY: {
			long durMilliSec = end.getTime() - start.getTime();
			return Math.round((double) durMilliSec / 1000 / 60 / 60 / 24);
		}
		case MONTH: {
			Calendar starCal = Calendar.getInstance();
			starCal.setTime(start);
			int sYear = starCal.get(Calendar.YEAR);
			int sMonth = starCal.get(Calendar.MONTH);
			int sDay = starCal.get(Calendar.DATE);

			Calendar endCal = Calendar.getInstance();
			endCal.setTime(end);
			int eYear = endCal.get(Calendar.YEAR);
			int eMonth = endCal.get(Calendar.MONTH);
			int eDay = endCal.get(Calendar.DATE);

			long monthDur = ((eYear - sYear) * 12 + (eMonth - sMonth));

			if (eDay - sDay > 15) {
				monthDur = monthDur + 1;
			}
			return monthDur;
		}
		case YEAR: {
			Calendar starCal = Calendar.getInstance();
			starCal.setTime(start);
			int sYear = starCal.get(Calendar.YEAR);
			int sMonth = starCal.get(Calendar.MONTH);

			Calendar endCal = Calendar.getInstance();
			endCal.setTime(end);
			int eYear = endCal.get(Calendar.YEAR);
			int eMonth = endCal.get(Calendar.MONTH);

			long yearDur = eYear - sYear;

			if (eMonth - sMonth > 6) {
				yearDur = yearDur + 1;
			}
			return yearDur;
		}
		default:
			return 0;
		}
	}

	public static String getFormatOfCurrentTime(String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);// 设置日期格式"yyyy-MM-dd HH:mm:ss"
		return df.format(new Date()); // new Date()为获取当前系统时间
	}

	public static enum Field {
		YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND;
	}
}
